package com.chen.servicemap.service;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.chen.internalcommon.constant.CommonStatusEnum;
import com.chen.internalcommon.constant.MapConfigConstants;
import com.chen.internalcommon.dto.ResponseResult;
import com.chen.servicemap.entity.DicDistrict;
import com.chen.servicemap.mapper.DisDistrictMapper;
import com.chen.servicemap.remote.MapDicDistrictlClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author chenLiuPing
 **/
@Service
public class DicDistrictService {

    @Autowired
    private MapDicDistrictlClient mapDicDistrictlClient;

    @Autowired
    private DisDistrictMapper disDistrictMapper;
    public ResponseResult initDicDistrict(String keywords) {
        // 请求地图
        String district = mapDicDistrictlClient.dicDistrict(keywords);
        // 解析结果
        JSONObject jsonObject = JSONObject.parseObject(district);
        int status = jsonObject.getIntValue(MapConfigConstants.STATUS);
        if (status == 1) {
            JSONArray countryJsonArray = jsonObject.getJSONArray(MapConfigConstants.DISTRICTS);
            // 所有国家
            for (int i = 0; i < countryJsonArray.size(); i++) {
                JSONObject countryObject = countryJsonArray.getJSONObject(0);
                String countryCode = insertDistrict("0", countryObject);
                // 国家下面所有的省份
                JSONArray provinceJsonArray = countryObject.getJSONArray(MapConfigConstants.DISTRICTS);
                for (int j = 0; j < provinceJsonArray.size(); j++) {
                    JSONObject provinceObject = provinceJsonArray.getJSONObject(j);
                    String provinceCode = insertDistrict(countryCode, provinceObject);
                // 省份下面所有的城市
                    JSONArray cityJsonArray = provinceObject.getJSONArray(MapConfigConstants.DISTRICTS);
                    for (int k = 0; k < cityJsonArray.size(); k++) {
                        JSONObject cityObject = cityJsonArray.getJSONObject(k);
                        String cityCode = insertDistrict(provinceCode, cityObject);
                        // 城市下面所有的区县
                        JSONArray districtJsonArray = cityObject.getJSONArray(MapConfigConstants.DISTRICTS);
                        for (int l = 0; l < districtJsonArray.size(); l++) {
                            JSONObject districtObject = districtJsonArray.getJSONObject(l);
                            insertDistrict(cityCode, districtObject);
                        }
                    }
                }

            }

        } else {
            return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR);
        }
        return ResponseResult.success();
    }

    private String insertDistrict(String parentAddressCode, JSONObject district) {
        String addressCode = district.getString(MapConfigConstants.ADCODE);
        String addressName = district.getString(MapConfigConstants.NAME);
        int level = parseLevel(district.getString(MapConfigConstants.LEVEL));
        if (level != -1) {
            DicDistrict dicDistrict = new DicDistrict(addressCode, addressName, parentAddressCode, level);
            disDistrictMapper.insert(dicDistrict);
        }
        return addressCode;
    }

    private int parseLevel(String level) {
        if (MapConfigConstants.COUNTRY.equals(level)) {
            return 0;
        }
        if (MapConfigConstants.PROVINCE.equals(level)) {
            return 1;
        }
        if (MapConfigConstants.CITY.equals(level)) {
            return 2;
        }
        if (MapConfigConstants.DISTRICT.equals(level)) {
            return 3;
        }
        // 因为部分城市和省直辖县因为没有区县的概念，故在市级下方直接显示街道。例如：广东-东莞、海南-文昌市
        // 其街道的adcode和所属城市的adcode一样
        if (MapConfigConstants.STREET.equals(level)) {
            return -1;
        }
        return -1;
    }
}
